package com.jumi.microservice.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.jwt.constant.JwtAdminInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.domain.HostSaleGoodsDTO;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.entity.JmUserStatisticsFenxiao;
import com.jumi.microservice.entity.JmUserStatisticsSales;
import com.jumi.microservice.entity.JmUserStatisticsTeam;
import com.jumi.microservice.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业绩，团队，收益排行榜
 * </p>
 *
 * @author 要挑客户管理团队
 * @since 2020-11-14
 */

/**
 * 业绩type
 * sales 总
 * sales_yesterday 昨日
 * sales_week 七日
 * sales_month  月
 * <p>
 * 收益type
 * fenxiao	总
 * fenxiao_yesterday	昨日
 * fenxiao_week	七日
 * fenxiao_month	月
 * <p>
 * 团队type
 * team 总
 * team_yesterday   昨日
 * team_week    七日
 * team_month   月
 */
@RestController
@RequestMapping("/api/rank")
@Api(value = "C-业绩团队收益排行榜", tags = {"C-业绩团队收益排行榜"})
public class SalesFenxiaoTeamController {

    @Autowired
    IJmUserStatisticsFenxiaoService fenxiaoService;

    @Autowired
    IJmUserStatisticsSalesService salesService;

    @Autowired
    IJmUserStatisticsTeamService teamService;

    @Autowired
    IJmUserService userService;

    @DubboReference(check = false)
    CommodityApi commodityApi;

    @PostMapping("/getSalesDeificationList")
    @ApiOperation("获取业绩排行榜")
    public ResponseResult getSalesDeificationList(String type) {
        Integer uid = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        JmUser byId = userService.checkUserExist(uid);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("pid", uid);
        wrapper.orderByDesc(type);
        wrapper.last("limit 50");

        List<JmUserStatisticsSales> sales = salesService.list(wrapper);

        if (ObjectUtil.isEmpty(sales)) return ResponseResult.success("没有任何数据！",sales);

        List<Map> salesList = new ArrayList<>();

        for (int i = 0; i < sales.size(); i++) {
            JmUserStatisticsSales sale = (JmUserStatisticsSales) sales.get(i);
            Map saleOne = new HashMap();
            // 查询昵称  头像 等
            QueryWrapper wrapperUser = new QueryWrapper();
            wrapperUser.eq("id", sale.getUid());
            JmUser one = userService.getOne(wrapperUser);

            saleOne.put("avatar", one.getAvatar());
            saleOne.put("mobile", one.getMobile());
            saleOne.put("name", one.getName());
            saleOne.put("level", one.getLevel());

            saleOne.put("uid", sale.getUid());

            switch (type) {

                case "sales":

                    saleOne.put("sales", sale.getSales());

                    break;
                case "sales_yesterday":

                    saleOne.put("sales", sale.getSalesYesterday());

                    break;
                case "sales_week":

                    saleOne.put("sales", sale.getSalesWeek());

                    break;
                case "sales_month":

                    saleOne.put("sales", sale.getSalesMonth());

                    break;

            }

            salesList.add(saleOne);
        }

        return ResponseResult.success(salesList);
    }

    @PostMapping("/getFenXiaoDeificationList")
    @ApiOperation("获取分销收益排行榜")
    public ResponseResult getFenXiaoDeificationList(String type) {
        Integer uid = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        JmUser byId = userService.checkUserExist(uid);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("pid", uid);
        wrapper.orderByDesc(type);
        wrapper.last("limit 50");

        List<JmUserStatisticsFenxiao> fenxiaos = fenxiaoService.list(wrapper);

        if (ObjectUtil.isEmpty(fenxiaos)) return ResponseResult.success("没有任何数据！",fenxiaos);

        List<Map> fenxiaoList = new ArrayList<>();

        for (int i = 0; i < fenxiaos.size(); i++) {
            JmUserStatisticsFenxiao fenxiao = (JmUserStatisticsFenxiao) fenxiaos.get(i);
            Map fenxiaoOne = new HashMap();
            // 查询昵称  头像 等
            QueryWrapper wrapperUser = new QueryWrapper();
            wrapperUser.eq("id", fenxiao.getUid());
            JmUser one = userService.getOne(wrapperUser);

            fenxiaoOne.put("avatar", one.getAvatar());
            fenxiaoOne.put("mobile", one.getMobile());
            fenxiaoOne.put("name", one.getName());
            fenxiaoOne.put("level", one.getLevel());

            fenxiaoOne.put("uid", fenxiao.getUid());

            switch (type) {

                case "fenxiao":

                    fenxiaoOne.put("fenxiao", fenxiao.getFenxiao());

                    break;
                case "fenxiao_yesterday":

                    fenxiaoOne.put("fenxiao", fenxiao.getFenxiaoYesterday());

                    break;
                case "fenxiao_week":

                    fenxiaoOne.put("fenxiao", fenxiao.getFenxiaoWeek());

                    break;
                case "fenxiao_month":

                    fenxiaoOne.put("fenxiao", fenxiao.getFenxiaoMonth());

                    break;

            }

            fenxiaoList.add(fenxiaoOne);
        }

        return ResponseResult.success(fenxiaoList);
    }

    @PostMapping("/getTeamDeificationList")
    @ApiOperation("获取团队排行榜")
    public ResponseResult getTeamDeificationList(String type) {
        Integer uid = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        JmUser byId = userService.checkUserExist(uid);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("pid", uid);
        wrapper.orderByDesc(type);
        wrapper.last("limit 50");

        List<JmUserStatisticsTeam> teams = teamService.list(wrapper);

        if (ObjectUtil.isEmpty(teams)) return ResponseResult.success("没有任何数据！",teams);

        List<Map> teamList = new ArrayList<>();

        for (int i = 0; i < teams.size(); i++) {
            JmUserStatisticsTeam team = (JmUserStatisticsTeam) teams.get(i);
            Map teamOne = new HashMap();
            // 查询昵称  头像 等
            QueryWrapper wrapperUser = new QueryWrapper();
            wrapperUser.eq("id", team.getUid());
            JmUser one = userService.getOne(wrapperUser);

            teamOne.put("avatar", one.getAvatar());
            teamOne.put("mobile", one.getMobile());
            teamOne.put("name", one.getName());
            teamOne.put("level", one.getLevel());

            teamOne.put("uid", team.getUid());

            switch (type) {

                case "team":

                    teamOne.put("team", team.getTeam());

                    break;
                case "team_yesterday":

                    teamOne.put("team", team.getTeamYesterday());

                    break;
                case "team_week":

                    teamOne.put("team", team.getTeamWeek());

                    break;
                case "team_month":

                    teamOne.put("team", team.getTeamMonth());

                    break;

            }

            teamList.add(teamOne);
        }

        return ResponseResult.success(teamList);
    }

    /**
     * 热销商品排行榜
     */
    @PostMapping("/getHotGoodsList")
    @ApiOperation("获取热销商品排行榜")
    @ApiImplicitParam(name = "rankSize",value = "排行榜上榜商品数量/前10,前50,etc. 默认前50")
    public ResponseResult<List<HostSaleGoodsDTO>> getHotGoodsList(Integer rankSize) {
        if (ObjectUtil.isNull(rankSize))
            rankSize = 50;
        List<HostSaleGoodsDTO> list = commodityApi.listHostSaleGoods(rankSize);
        return ResponseResult.success(list);
    }

}
